import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_fortune_wheel/flutter_fortune_wheel.dart';
import 'package:yp/widgets/dialog.dart';
import 'package:yp/widgets/index.dart';

/// 大转盘  没有具体界面 单独拿出来做了一个
class RoundAbout extends StatefulWidget {
  const RoundAbout({Key key}) : super(key: key);

  @override
  _RoundAboutState createState() => _RoundAboutState();
}

class _RoundAboutState extends State<RoundAbout>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  StreamController<int> _streamController = StreamController<int>();

  final items = <String>[
    '100',
    '200',
    '500',
    '666',
    '888',
    '999',
    '1999',
    '9999'
  ];

  int _giftNum = 0;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _streamController.close();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: YPNavigationBar('大转盘'),
        body: Center(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              SizedBox(
                width: 300,
                height: 300,
                child: FortuneWheel(
                  /// 最终选中的值
                  selected: _streamController.stream,
                  animateFirst: false,
                  // rotationCount: 10,
                  // 禁用手动旋转
                  // physics: NoPanPhysics(),
                  indicators: [
                    FortuneIndicator(
                      child: Container(
                          width: 40,
                          height: 40,
                          child: Image.asset(
                              'assets/images/roulette-center-300.png')),
                    )
                  ],
                  onAnimationStart: () {
                    print('开始旋转');
                  },

                  onFling: () {
                    print('手动旋转');
                  },
                  onAnimationEnd: () {
                    print('旋转结束');
                    if (_giftNum == 1 || _giftNum == 3) {
                      /// 中奖了
                      // YpShowDialog.showCongratulations(context, () {
                      //   print('查看奖品');
                      // });
                    } else {
                      /// 没中奖
                      // YpShowDialog.showSorry(context);
                    }
                  },
                  items: [
                    for (var it in items)
                      FortuneItem(
                        child: Text('￥' + it),
                        // style: FortuneItemStyle(
                        //     color: Colors.green,
                        //     borderColor: Colors.red,
                        //     borderWidth: 3
                        //     )
                      ),
                  ],
                ),
              ),
              SizedBox(height: 20),
              OutlinedButton(
                  onPressed: () {
                    /// 0-length 5就是999
                    _giftNum = _random();
                    _streamController.sink.add(_giftNum);
                  },
                  child: Text('抽奖')),
              // Text('tips:看到这个功能 不知道在哪里放 此界面仅做展示')
            ],
          ),
        ));
  }

  int _random() {
    return Fortune.randomInt(0, items.length);
  }
}
